<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="50%"
    class="common-dialog"
  >
    <el-form
      ref="form"
      :model="form"
      label-width="100px"
      label-suffix="："
    >
      <el-form-item label="司机名称">{{driver.username}}</el-form-item>
      <el-form-item label="手机号码">{{driver.phone}}</el-form-item>
      <el-form-item
        label="充值金额"
        prop="money"
        :rules="[{required: true, message: '请输入充值金额'}]"
      >
        <el-input
          size="small"
          v-model="form.money"
          placeholder="请输入充值金额"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button
        size="small"
        @click="dialogVisible = false"
      >
        取 消
      </el-button>
      <el-button
        size="small"
        type="primary"
        :loading="submit_loading"
        @click="recharge"
      >
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  /**
   * @Author: lifuzhao
   * @Date: 2019-07-29
   * @Project: driver
   */
  export default {
    name: "recharge"
    , data() {
      return {
        dialogVisible: false
        , driver: {}
        , form: {
          d_id: ''
          , money: ''
        }
        , submit_loading: false
      }
    }
    , computed: {
      title() {
        let driver = this.driver
        return '正在为 ' + driver.username + ' 充值'
      }
    }
    , watch: {
      dialogVisible(bool) {
        this.$fn.setTitle(bool ? this.title : '账号管理')
      }
    }
    , mounted() {
      this.init()
    }
    , methods: {
      init() {
        this.$bus.$on('rechargeAccount', driver => {
          this.dialogVisible = true
          this.driver = driver
          this.form.d_id = driver.id
        })
      }
      , recharge() {
        this.$refs['form'].validate(valid => {
          if (!valid) return
          this.submit_loading = true
          this.$axios.post('/api/v1/recharge/save', this.form)
            .then(res => {
              if (res.success) {
                this.$message({
                  type: 'success'
                  , message: '充值成功'
                })
                this.dialogVisible = false
                this.$bus.$emit('updateAccountManageList')
              } else {
                this.$message({
                  type: 'error'
                  , message: '充值失败' + res.msg
                })
              }
            })
            .catch(() => {
              this.$message({
                type: 'error'
                , message: '充值失败，请稍后再试'
              })
            })
            .finally(() => {
              this.submit_loading = false
            })
        })
      }
    }
  }
</script>

<style scoped>

</style>